package com.he.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.he.pojo.Role;
import com.he.pojo.RoleExample;
import com.he.pojo.Step;
import com.he.pojo.StepExample;
import com.he.pojo.StepExample.Criteria;
import com.he.pojo.User;
import com.he.pojo.UserExample;
import com.he.pojo.Workflow;
import com.he.service.role.RoleServiceI;
import com.he.service.step.StepServiceI;
import com.he.service.user.UserServiceI;
import com.he.service.workflow.WorkflowServiceI;

@Controller
@RequestMapping("step")
public class StepController {

	@Autowired
	private StepServiceI ssi;
	@Autowired
	private WorkflowServiceI wfi;
	@Autowired
	private UserServiceI usi;
	@Autowired
	private RoleServiceI rsi;
	
	@RequestMapping("show")//查看步骤
	public ModelAndView showstep(Integer id){
		System.out.println(id);
		ModelAndView mv=new ModelAndView("flowstep");
//		StepExample stepExample=new StepExample();
//		Criteria createCriteria = stepExample.createCriteria();
//		createCriteria.andWorkflowIdEqualTo(id);
//		Step step=new Step();
		List<Step> list=ssi.findAllBySort(id);
		for (Step stp : list) {		
			stp.setWorkflow(wfi.getWorkflowById(stp.getWorkflowId()));
			if (stp.getUserId()!=null) {
				stp.setUser(usi.getUserById(stp.getUserId()));
			}	
		}		
		mv.addObject("list", list);
		mv.addObject("workflowid", id);
		return mv;
	}
	@RequestMapping("addstep")//添加步骤
	public ModelAndView addStep(Integer workfowid){
		ModelAndView mv=new ModelAndView("addstep");
		Workflow workflow=wfi.getWorkflowById(workfowid);
		mv.addObject("workflow", workflow);//获得所属流程
		List<Step> list=new ArrayList<>();
		StepExample stepExample=new StepExample();
		Step step=new Step();
		Criteria createCriteria = stepExample.createCriteria();
		createCriteria.andWorkflowIdEqualTo(workfowid);
		list=ssi.getSteps(stepExample, step);//获得指定流程下的所有步骤
		mv.addObject("list", list);
		
		RoleExample roleExample=new RoleExample();//获得经理角色
		com.he.pojo.RoleExample.Criteria rcc = roleExample.createCriteria();
		rcc.andRoleNameLike("%经理%");
		Role role=new Role();
		List<Role> rlist=rsi.getRoles(roleExample, role);
		
		List<User> ulist=new ArrayList<>();
		User user=new User();
		for (Role r : rlist) {
			UserExample userExample = new UserExample();//获得审核人list
			com.he.pojo.UserExample.Criteria ucc = userExample.createCriteria();
			ucc.andRoleIdEqualTo(r.getId());
			List<User> uulist=usi.getUsers(userExample, user);
			for (User user2 : uulist) {
				ulist.add(user2);
			}
		}
		mv.addObject("ulist", ulist);		
		return mv;
	}
	@RequestMapping("addsave")//保存添加的步骤信息，并更新序号
	@ResponseBody
	public String addSave(Step step){
		StepExample stepExample=new StepExample();
		Criteria createCriteria = stepExample.createCriteria();
		createCriteria.andWorkflowIdEqualTo(step.getWorkflowId());
		Step stp=new Step();
		List<Step> list=ssi.getSteps(stepExample, stp);
		if (step.getStepNum()==0) {
			step.setStepNum(1);
			if (list!=null) {
				for (Step sstp : list) {
					int r=sstp.getStepNum();
					sstp.setStepNum(r+1);
				}
			}
		}else{
			int s=step.getStepNum();
			step.setStepNum(s+1);
			for (Step ssstp : list) {
				int zero=ssstp.getStepNum();
				if (zero>=step.getStepNum()) {
					ssstp.setStepNum(zero+1);
				}
			}
		}
		int rs=0;
		for (Step stpp : list) {
			rs=rs+ssi.update(stpp);
		}
		rs=rs+ssi.insert(step);
		Integer workflowid=step.getWorkflowId();
		String id=Integer.toString(workflowid);
		if (rs==list.size()+1) {
			return id;
		} else {
			return "0";
		}
	}
	@RequestMapping("editstep")//修改流程，目前阶段暂不支持修改序号。
	public ModelAndView editStep(Integer id){
		ModelAndView mv=new ModelAndView("stepedit");
		Step step=ssi.getStepById(id);
		
		mv.addObject("step", step);
		
		if (step.getUserId()!=null) {
			step.setUser(usi.getUserById(step.getUserId()));
		}
		
		RoleExample roleExample=new RoleExample();//获得经理角色
		com.he.pojo.RoleExample.Criteria rcc = roleExample.createCriteria();
		rcc.andRoleNameLike("%经理%");
		Role role=new Role();
		List<Role> rlist=rsi.getRoles(roleExample, role);
		
		List<User> ulist=new ArrayList<>();
		User user=new User();
		for (Role r : rlist) {
			UserExample userExample = new UserExample();//获得审核人list
			com.he.pojo.UserExample.Criteria ucc = userExample.createCriteria();
			ucc.andRoleIdEqualTo(r.getId());
			List<User> uulist=usi.getUsers(userExample, user);
			for (User user2 : uulist) {
				ulist.add(user2);
			}
		}
		mv.addObject("ulist", ulist);
		
		return mv;
	}
	@RequestMapping("editsave")
	@ResponseBody
	public String editSave(Step step){
//		System.out.println(step.getId()+" "+step.getStepName()+" "+step.getUserId());
//		StepExample stepExample=new StepExample();
//		Criteria createCriteria = stepExample.createCriteria();
//		createCriteria.andIdEqualTo(step.getId());
//		createCriteria.andStepNameEqualTo(step.getStepName());
//		createCriteria.andStepInfoEqualTo(step.getStepInfo());
//		createCriteria.andUserIdEqualTo(step.getUserId());
		int r=ssi.update(step);
		if (r>0) {
			return Integer.toString(step.getWorkflowId());
		} else {
			return "0";
		}
	}
	@RequestMapping("deletstep")
	@ResponseBody
	public String deletStep(Integer id){
		//获得所要删除的step实体
		Step step=ssi.getStepById(id);
		Integer workflowId=step.getWorkflowId();//用于返回值
		
		//查询所属流程的所有步骤
		StepExample stepExample=new StepExample();
		Criteria createCriteria = stepExample.createCriteria();
		createCriteria.andWorkflowIdEqualTo(step.getWorkflowId());
		List<Step> list=ssi.getSteps(stepExample, new Step());
		//修改序号
		for (Step stp : list) {
			if (stp.getStepNum()>step.getStepNum()) {
				int num=stp.getStepNum();
				stp.setStepNum(num-1);
				ssi.update(stp);
			}
		}
		//删除
		int r=ssi.delete(id);
		if (r>0) {
			return Integer.toString(workflowId);
		} else {
			return "0";
		}
	}
}
